package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PcbaDeviceTypeServiceImpl implements PcbaDeviceTypeService {

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Autowired
    private PcbaDeviceParamTypeDao pcbaDeviceParamTypeDao;

    @Autowired
    private TechDeviceParamTypeDao techDeviceParamTypeDao;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private BrandTypeDao brandTypeDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private OperateService operateService;

    @Autowired
    private Base64Dao base64Dao;

    @Value("${personal.config.amazon-s3.url}")
    private String url;
    @Value("${personal.config.amazon-s3.file-bucket}")
    private String fileBucket;
    @Value("${personal.config.env}")
    private String env;

    private String operateSmallType = "设备类型管理";

    @Override
    public List<PcbaDeviceType> getAllPcbaDeviceType(String searchValue, Integer isTechOwn) {
        List<PcbaDeviceType> list = pcbaDeviceTypeDao.getAllPcbaDeviceType(searchValue, isTechOwn);
        for (PcbaDeviceType pcbaDeviceType : list) {

            List<PcbaDeviceResource> allPcbaDeviceResource = pcbaDeviceResourceDao.getAllByDeviceTypeAndSearch("", pcbaDeviceType.getPcbaDeviceTypeId(), null, isTechOwn, 1, null, null, null, null, null, null, null);
            for (PcbaDeviceResource pcbaDeviceResource : allPcbaDeviceResource) {
                pcbaDeviceResource.setBrand(brandDao.getBrandById(pcbaDeviceResource.getBrandId()));
                pcbaDeviceResource.setBrandType(brandTypeDao.getBrandTypeById(pcbaDeviceResource.getBrandTypeId()));
            }
            pcbaDeviceType.setPcbaDeviceResourceList(allPcbaDeviceResource);

            List<PcbaDeviceParamType> allPcbaDeviceParamType = pcbaDeviceParamTypeDao.getPcbaDeviceParamTypeByPcbaDeviceTypeId(pcbaDeviceType.getPcbaDeviceTypeId(), "");
            for (PcbaDeviceParamType pcbaDeviceParamType : allPcbaDeviceParamType) {
                pcbaDeviceParamType.setPcbaDeviceParamList(pcbaDeviceParamDao.getAllPcbaDeviceParam(pcbaDeviceParamType.getPcbaDeviceTypeId(), pcbaDeviceParamType.getPcbaDeviceParamTypeId(), ""));
            }
            pcbaDeviceType.setPcbaDeviceParamTypeList(allPcbaDeviceParamType);

            List<TechDeviceParamType> allTechDeviceParamType = techDeviceParamTypeDao.getAllTechDeviceParamTypeByDeviceType(pcbaDeviceType.getPcbaDeviceTypeId(), "");
            pcbaDeviceType.setTechDeviceParamTypeList(allTechDeviceParamType);
        }
        return list;
    }

    @Override
    public List<PcbaDeviceType> getAllPcbaDeviceTypeWithPic(String searchValue, Integer isTechOwn) {
        List<PcbaDeviceType> list = pcbaDeviceTypeDao.getAllPcbaDeviceType(searchValue, isTechOwn);
        for (PcbaDeviceType pcbaDeviceType : list) {

            if(pcbaDeviceType != null){
                if(pcbaDeviceType.getDevicePicture() != null && !"".equals(pcbaDeviceType.getDevicePicture())){
                    if("h3c_pro".equals(env)){
                        //pcbaDeviceType.setDevicePictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(url + "/" + fileBucket + "/" + pcbaDeviceType.getDevicePicture()));
                        List<Base64> base64ByTypeAndOuterId = base64Dao.getBase64ByTypeAndOuterId(1, pcbaDeviceType.getPcbaDeviceTypeId());
                        if(base64ByTypeAndOuterId != null && base64ByTypeAndOuterId.size() > 0){
                            String base64 = base64ByTypeAndOuterId.get(0).getBase64();
                            if(base64 != null && !"".equals(base64)){
                                pcbaDeviceType.setDevicePictureBase64(base64);
                            }
                        }
                    }else{
                        List<Base64> base64ByTypeAndOuterId = base64Dao.getBase64ByTypeAndOuterId(1, pcbaDeviceType.getPcbaDeviceTypeId());
                        if(base64ByTypeAndOuterId != null && base64ByTypeAndOuterId.size() > 0){
                            String base64 = base64ByTypeAndOuterId.get(0).getBase64();
                            if(base64 != null && !"".equals(base64)){
                                pcbaDeviceType.setDevicePictureBase64(base64);
                            }
                        }
                        //pcbaDeviceType.setDevicePictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(CommonUtils.returnHostPrefixUrl() + pcbaDeviceType.getDevicePicture()));
                    }
                }
            }

            List<PcbaDeviceResource> allPcbaDeviceResource = pcbaDeviceResourceDao.getAllByDeviceTypeAndSearch("", pcbaDeviceType.getPcbaDeviceTypeId(), null, isTechOwn, 1, null, null, null, null, null, null, null);
            for (PcbaDeviceResource pcbaDeviceResource : allPcbaDeviceResource) {
                pcbaDeviceResource.setBrand(brandDao.getBrandById(pcbaDeviceResource.getBrandId()));
                pcbaDeviceResource.setBrandType(brandTypeDao.getBrandTypeById(pcbaDeviceResource.getBrandTypeId()));
            }
            pcbaDeviceType.setPcbaDeviceResourceList(allPcbaDeviceResource);

            List<PcbaDeviceParamType> allPcbaDeviceParamType = pcbaDeviceParamTypeDao.getPcbaDeviceParamTypeByPcbaDeviceTypeId(pcbaDeviceType.getPcbaDeviceTypeId(), "");
            for (PcbaDeviceParamType pcbaDeviceParamType : allPcbaDeviceParamType) {
                pcbaDeviceParamType.setPcbaDeviceParamList(pcbaDeviceParamDao.getAllPcbaDeviceParam(pcbaDeviceParamType.getPcbaDeviceTypeId(), pcbaDeviceParamType.getPcbaDeviceParamTypeId(), ""));
            }
            pcbaDeviceType.setPcbaDeviceParamTypeList(allPcbaDeviceParamType);

            List<TechDeviceParamType> allTechDeviceParamType = techDeviceParamTypeDao.getAllTechDeviceParamTypeByDeviceType(pcbaDeviceType.getPcbaDeviceTypeId(), "");
            pcbaDeviceType.setTechDeviceParamTypeList(allTechDeviceParamType);
        }
        return list;
    }

    @Override
    public Result deletePcbaDeviceType(Integer pcbaDeviceTypeId) {
        try {
            if (pcbaDeviceTypeId == null || "".equals(pcbaDeviceTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceTypeId", "Miss Param pcbaDeviceTypeId", null);
            }
            // 判断是否有关联，是否允许删除
            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeletePcbaDeviceType(pcbaDeviceTypeId);
            if (stringIntegerMap.get("pcbaDeviceResourceNum") > 0 || stringIntegerMap.get("pcbaDeviceParamTypeNum") > 0 || stringIntegerMap.get("techDeviceParamTypeNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("pcbaDeviceParamTypeNum") + "处关联基线大类，" +
                                stringIntegerMap.get("techDeviceParamTypeNum") + "处关联工艺大类，" +
                                stringIntegerMap.get("pcbaDeviceResourceNum") + "处关联设备资源",
                        "Forbidden Delete", null);
            }

            String userId = UserHeaderHolder.getUserId();
            PcbaDeviceType oldPcbaDeviceType = pcbaDeviceTypeDao.getById(pcbaDeviceTypeId);

            Integer integer = pcbaDeviceTypeDao.deletePcbaDeviceType(pcbaDeviceTypeId);
            if (integer > 0) {

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldPcbaDeviceType), null));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdatePcbaDeviceType(PcbaDeviceType pcbaDeviceType, String mode) {
        try {
            if (pcbaDeviceType == null) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceType", "Miss Param pcbaDeviceType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (pcbaDeviceType.getPcbaDeviceTypeId() == null || "".equals(pcbaDeviceType.getPcbaDeviceTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数pcbaDeviceTypeId", "Miss Param pcbaDeviceTypeId", null);
                }

                PcbaDeviceType oldPcbaDeviceType = pcbaDeviceTypeDao.getById(pcbaDeviceType.getPcbaDeviceTypeId());

                List<PcbaDeviceType> pcbaDeviceTypes = pcbaDeviceTypeDao.judgeUniquePcbaDeviceType(pcbaDeviceType.getPcbaDeviceTypeNameCn(), pcbaDeviceType.getPcbaDeviceTypeId());
                if (pcbaDeviceTypes.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                pcbaDeviceType.setUpdateTime(new Date());
                Integer integer = pcbaDeviceTypeDao.updatePcbaDeviceType(pcbaDeviceType);
                if (integer > 0) {

                    // 添加base64
                    List<Base64> base64ByTypeAndOuterId = base64Dao.getBase64ByTypeAndOuterId(1, pcbaDeviceType.getPcbaDeviceTypeId());
                    if(base64ByTypeAndOuterId == null || base64ByTypeAndOuterId.size() == 0){
                        // add
                        Base64 addBase64 = new Base64();
                        addBase64.setBase64(pcbaDeviceType.getDevicePictureBase64());
                        addBase64.setOuterId(pcbaDeviceType.getPcbaDeviceTypeId());
                        addBase64.setType(1);
                        addBase64.setCreateTime(new Date());
                        base64Dao.addBase64(addBase64);
                    }else{
                        // update
                        Base64 updateBase64 = new Base64();
                        updateBase64.setBase64(pcbaDeviceType.getDevicePictureBase64());
                        updateBase64.setOuterId(pcbaDeviceType.getPcbaDeviceTypeId());
                        updateBase64.setBase64Id(base64ByTypeAndOuterId.get(0).getBase64Id());
                        base64Dao.updateBase64(updateBase64);
                    }

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldPcbaDeviceType), JSONObject.toJSONString(pcbaDeviceType)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {
                List<PcbaDeviceType> pcbaDeviceTypes = pcbaDeviceTypeDao.judgeUniquePcbaDeviceType(pcbaDeviceType.getPcbaDeviceTypeNameCn(), null);
                if (pcbaDeviceTypes.size() > 0) {
                    return ResultUtil.error(500, "添加失败，存在相同名字", "Operate Fail", null);
                }

                pcbaDeviceType.setCreateTime(new Date());
                Integer integer = pcbaDeviceTypeDao.addPcbaDeviceType(pcbaDeviceType);
                if (integer > 0) {

                    // 添加base64
                    List<Base64> base64ByTypeAndOuterId = base64Dao.getBase64ByTypeAndOuterId(1, pcbaDeviceType.getPcbaDeviceTypeId());
                    if(base64ByTypeAndOuterId == null || base64ByTypeAndOuterId.size() == 0){
                        // add
                        Base64 addBase64 = new Base64();
                        addBase64.setBase64(pcbaDeviceType.getDevicePictureBase64());
                        addBase64.setOuterId(pcbaDeviceType.getPcbaDeviceTypeId());
                        addBase64.setType(1);
                        addBase64.setCreateTime(new Date());
                        base64Dao.addBase64(addBase64);
                    }else{
                        // update
                        Base64 updateBase64 = new Base64();
                        updateBase64.setBase64(pcbaDeviceType.getDevicePictureBase64());
                        updateBase64.setOuterId(pcbaDeviceType.getPcbaDeviceTypeId());
                        updateBase64.setBase64Id(base64ByTypeAndOuterId.get(0).getBase64Id());
                        base64Dao.updateBase64(updateBase64);
                    }

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(pcbaDeviceType)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public PcbaDeviceType getById(Integer pcbaDeviceTypeId) {
        return pcbaDeviceTypeDao.getById(pcbaDeviceTypeId);
    }
}
